﻿using System;

using Android.App;
using Android.Content.PM;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content.Res;
using SkiaSharp;
using System.IO;
using Android.Util;

namespace Demo.Droid
{
    [Activity(Label = "Demo", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics);
            App.Density = displayMetrics.Density;

            AssetManager assets = this.Assets;
            SKTypeface font = SKTypeface.FromStream(assets.Open("msyh.ttf"));
            App.Typeface = font;

            string html = "";
            using (var streamReader = new StreamReader(assets.Open("00.Intro.htm")))
            {
                html = streamReader.ReadToEnd();
            }
            App.HTML = html;

            var fontManager = SKFontManager.Default;
            for (var i = fontManager.FontFamilyCount - 1; i >= 0; i--)
            {
                Console.WriteLine("font=" + fontManager.GetFamilyName(i));
            }

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }

        public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
        {
            Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}